//
//  SDProductRecommendVC.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDProductRecommendVC.h"
#import "SDHomeHeaderView.h"

#import "SDProductRecommendView.h"
#import "SDProductListCell.h"

#import "SDDiagnoseSkinData.h"
#import "SDDetectionDataModel.h"

#import "SDProductDetailVC.h"

#import "SDHomeProductData.h"
#import "SDHomeProductList.h"
#import "SDHomeProductModel.h"

#import "SDDetectionReportVC.h"
#import "SDQuestionnaireViewController.h"

static NSString *const cellIden = @"SDProductListCell";
static NSString *const ReusableViewIden = @"SDProductRecommendView";

@interface SDProductRecommendVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong)SDHomeProductModel *showModel;
@end

@implementation SDProductRecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navHeight.constant = SDNavBar_Height;
    if (self.isFromReport == NO)self.completeBtn.hidden = [SDDetectionDataModel shareInstance].detectionReportDic.count != 5;
    
    [self.collectionView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellWithReuseIdentifier:cellIden];
    [self.collectionView registerNib:[UINib nibWithNibName:ReusableViewIden bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden];
    [self loadData];
    
}
- (void)loadData
{
    
    [SDNetwork getProductRecommendAllWithSkinSubIdStr:self.typeStr ByUser:NO Finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
            self.showModel = [SDHomeProductModel modelObjectWithDictionary:dic];
            [self.collectionView reloadData];
        }
    }];
    
}

//- (void)loadData1
//{
//
//    [SDNetwork getProductRecommendWithSkinSubId:self.typeStr ByUser:NO Finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
//        if (!error) {
//            self.showModel = [SDHomeProductModel modelObjectWithDictionary:dic];
//            [self.collectionView reloadData];
//        }
//
//    }];
//
//}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.showModel.data.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SDHomeProductData *data = self.showModel.data[section];
    return data.list.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDProductListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden forIndexPath:indexPath];
    SDHomeProductData *data = self.showModel.data[indexPath.section];
    cell.list = data.list[indexPath.row];

    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SDMainScreenWidth - 45)/2.0, 78 + (SDMainScreenWidth - 45 - 60)/2.0);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 15, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SDHomeProductData *data = self.showModel.data[indexPath.section];
    if (kind == UICollectionElementKindSectionHeader) {

        SDProductRecommendView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ReusableViewIden forIndexPath:indexPath];
        [headerView.titleLab setText:data.typeName];
        return headerView;
       
    }

    return nil;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{

    return CGSizeMake(SDMainScreenWidth, 52);
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDHomeProductData *data = self.showModel.data[indexPath.section];
    SDHomeProductList *list = data.list[indexPath.row];
    SDProductDetailVC *vc =[SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDProductDetailVC class])];
    vc.list = list.bImg;
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)goDetectionReport:(UIButton *)sender {
    
    if ([SDDetectionDataModel shareInstance].userModel.skinType == 0) {
        [self goQuestionnaireVC];
    }else{
        sender.userInteractionEnabled = NO;
        [self createReportWithBlock:^(BOOL userEnable) {
            sender.userInteractionEnabled = YES;
        }];
    }
    
}
- (void)createReportWithBlock:(void(^)(BOOL userEnable))block
{
    [SDNetwork postGenerateReportFinish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
       // LogString([SDNetwork dicToJSONWith:dic]);
        
        block(YES);
        if (!error) {
            SDDetectionReportVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDDetectionReportVC class])];
            vc.reportId = [dic[@"data"] intValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
    
}
- (void)goQuestionnaireVC
{
    SDQuestionnaireViewController *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDQuestionnaireViewController class])];
    vc.isFromDetection = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
@end
